<?php
/*
#------------------------------------------------------------------------
  T3 Framework for Joomla 1.5
#------------------------------------------------------------------------
#Copyright (C) 2004-2009 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
#@license - GNU/GPL, http://www.gnu.org/copyleft/gpl.html
#Author: J.O.O.M Solutions Co., Ltd
#Websites: http://www.joomlart.com - http://www.joomlancers.com
#------------------------------------------------------------------------
*/


defined( '_VALID_MOS' ) or defined('_JEXEC') or die('Restricted access');
if (!defined ('_JA_MEGA_MENU_CLASS')) {
	define ('_JA_MEGA_MENU_CLASS', 1);
	require_once (dirname(__FILE__).DS."base.class.php");

	class JAMenuMega extends JAMenuBase{

		function __construct (&$params) {
			$params->set('megamenu', 1);
			parent::__construct($params);
		}

		function beginMenu($startlevel=0, $endlevel = 10){
			echo "<div class=\"ja-megamenu\">\n";
		}
		function endMenu($startlevel=0, $endlevel = 10){
			echo "\n</div>";
		}
		function beginMenuItems($pid=0, $level=0, $return=false){
			if ($level) {
				if ($this->items[$pid]->megaparams->get('group')) {
					echo "<div class=\"group-content\">";
				} else {
					$cols = $pid && $this->getParam('megamenu') && isset($this->items[$pid]->cols) && $this->items[$pid]->cols ? $this->items[$pid]->cols : 1;
					
					$width = $this->items[$pid]->megaparams->get('width', 0);
					if (!$width) {
						for ($col=0;$col<$cols;$col++) {
							$colw = $this->items[$pid]->megaparams->get('colw'.($col+1), 0);
							if (!$colw) $colw = $this->items[$pid]->megaparams->get('colw', $this->getParam ('mega-colwidth',200));
							$width += $colw;
						}
					}
					$style = $width?" style=\"width: {$width}px;\"":"";
					$right = $this->items[$pid]->megaparams->get('right') ? 'right':'';
					$data = "<div class=\"childcontent cols$cols $right\"$style><div class=\"childcontent-inner clearfix\">";
					if ($return) return $data; else echo $data;
				}
			}
		}
		function endMenuItems($pid=0, $level=0, $return=false){
			if ($level) {
				if ($this->items[$pid]->megaparams->get('group')) {
					$data = "</div>";
				}else{
					$data = "</div></div>";
				}
				if ($return) return $data; else echo $data;
			}
		}			
		function beginSubMenuItems($pid=0, $level=0, $pos, $i, $return = false){
			$data = '';
			if (isset ($this->items[$pid]) && $level) {
				if ($this->items[$pid]->megaparams->get('group')) {
				}else {
					$colw = $this->items[$pid]->megaparams->get('colw'.($i+1), 0);
					if (!$colw) $colw = $this->items[$pid]->megaparams->get('colw', $this->getParam ('mega-colwidth',200));
					$style = $colw?" style=\"width: {$colw}px;\"":"";
					$data .= "<div class=\"megacol column".($i+1).($pos?" $pos":"")."\"$style>";
				}
			}
			if (@$this->children[$pid]) $data .= "<ul class=\"megamenu level$level\">";
			if ($return) return $data; else echo $data;
		}
		function endSubMenuItems($pid=0, $level=0, $return = false){
			$data = '';
			if (@$this->children[$pid]) $data .= "</ul>";
			if (isset ($this->items[$pid]) && $level) {
				if ($this->items[$pid]->megaparams->get('group')) {
				}else
					$data .= "</div>";
			}
			if ($return) return $data; else echo $data;
		}
		
		function beginSubMenuModules($item, $level=0, $pos, $i, $return = false){
			$data = '';
			if ($level) {
				if ($item->megaparams->get('group')) {
				}else {
					$colw = $item->megaparams->get('colw'.($i+1), 0);
					if (!$colw) $colw = $item->megaparams->get('colw', $this->getParam ('mega-colwidth',200));
					$style = $colw?" style=\"width: {$colw}px;\"":"";
					$data .= "<div class=\"megacol column".($i+1).($pos?" $pos":"")."\"$style>";
				}
			}
			if ($return) return $data; else echo $data;
		}
		
		function endSubMenuModules($item, $level=0, $return = false){
			$data = '';
			if ($level) {
				if ($item->megaparams->get('group')) {
				}else
					$data .= "</div>";
			}
			if ($return) return $data; else echo $data;
		}

		function genClass ($mitem, $level, $pos) {
			$iParams = new JParameter ( $mitem->params );
			$active = in_array($mitem->id, $this->open);
			$cls = "mega".($active?" active":"").($pos?" $pos":"");
			if (@$this->children[$mitem->id] || (isset($mitem->content) && $mitem->content)) {
				if ($mitem->megaparams->get('group')) $cls .= " group";
				else if ($level < $this->getParam('endlevel')) $cls .= " haschild";
			}
			if ($mitem->megaparams->get('class')) $cls .= " ".$mitem->megaparams->get('class');
			return $cls?"class=\"$cls\"":"";
		}
		
		function beginMenuItem($mitem=null, $level = 0, $pos = ''){
			$active = $this->genClass ($mitem, $level, $pos);
			echo "<li $active>";
			if ($mitem->megaparams->get('group')) echo "<div class=\"group\">";
		}
		function endMenuItem($mitem=null, $level = 0, $pos = ''){
			if ($mitem->megaparams->get('group')) echo "</div>";
			echo "</li>";
		}		
	}
}